﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;
using Gift.Model.ENUM;
using Gift.DAL;
using Gift.Model;

namespace WebSite.admin.member
{
    public partial class member_msg : BaseAdmin
    {


        readonly DALMemberMsg theCommentMsg = new DALMemberMsg();
        readonly DALMember theMember = new DALMember();
        tbMember oMember;
        protected void Page_Load(object sender, EventArgs e)
        {


            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(ERule.UserComment);
                btnBrachDeleted.OnClientClick = Grid1.GetNoSelectionAlertInTopReference("没有选中项！");
                this.BindMsgTypeList();
                this.BindGrid();

            }
        }
        private void BindGrid()
        {

            DataTable table = theCommentMsg.GetList(GetSqlWhere).Tables[0];
            Grid1.DataSource = table;
            Grid1.DataBind();

        }

        string GetSqlWhere
        {
            get
            {
                StringBuilder sbWhere = new StringBuilder();
                sbWhere.Append(" 1=1 ");
                if (this.ddlMsgType.SelectedItem.Value.ToString() != "-1")
                {
                    sbWhere.AppendFormat(" and msgtypeid={0}", this.ddlMsgType.SelectedItem.Value);
                }
                if (this.txtKeywords.Text.ToString() != "")
                {
                    sbWhere.AppendFormat(" and title like '%{0}%' ",this.txtKeywords.Text.Trim());
                }
                  if (this.ddlProcessStates.SelectedItem.Value.ToString() != "-1")
                {
                    sbWhere.AppendFormat(" and isnull(states,0) ={0}", this.ddlProcessStates.SelectedItem.Value);
                }
               return sbWhere.ToString();
            }
        }



        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.BindGrid();
        }

        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;

        }



        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            DataTable table = theCommentMsg.GetList("").Tables[0];
            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);
            Grid1.DataSource = view1;
            Grid1.DataBind();
        }

        protected void btnBrachDeleted_Click(object sender, EventArgs e)
        {

            int selectedCount = this.Grid1.SelectedRowIndexArray.Length;
            StringBuilder sbComments = new StringBuilder();
            for (int i = 0; i < selectedCount; i++)
            {
                int rowIndex = this.Grid1.SelectedRowIndexArray[i];
                if (i == selectedCount - 1)
                {
                    sbComments.Append(this.Grid1.DataKeys[rowIndex][0].ToString());
                }
                else
                {
                    sbComments.Append(this.Grid1.DataKeys[rowIndex][0].ToString() + ",");
                }
            }
            if (sbComments.Length > 0)
            {
                theCommentMsg.DeleteList(sbComments.ToString());
            }
            ExtAspNet.Alert.Show("删除留言信息成功！");
            this.BindGrid();
        }

        protected void Window1_Close(object sender, EventArgs e)
        {
            this.BindGrid();
        }




        void BindMsgTypeList()
        {
            DataTable table = theCommentMsg.GetMsgTypeList().Tables[0];
            this.ddlMsgType.DataTextField = "msgtype";
            this.ddlMsgType.DataValueField = "msgtypeid";
            this.ddlMsgType.DataSource = table;
            this.ddlMsgType.DataBind();
            this.ddlMsgType.Items.Insert(0, new ExtAspNet.ListItem("选择留言类别...", "-1"));
            this.ddlMsgType.SelectedValue = "-1";

        }


      
        protected void Grid1_RowDataBound(object sender, ExtAspNet.GridRowEventArgs e)
        {
         //   DataRowView row = e.DataItem as DataRowView;
            if (e.Values[1].ToString() == "0")
            {
                e.Values[1] = "匿名用户";
            }
            else
            {
                oMember = theMember.GetModel(int.Parse(e.Values[1].ToString()));
                if (oMember != null)
                {
                    e.Values[1] = oMember.memberlogin;
                }
            }
        
        }

        protected void Grid1_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            if (e.CommandName == "deleted" )
            {
                theCommentMsg.Delete(int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString()));
                ExtAspNet.Alert.Show("删除留言信息成功！");
                this.BindGrid();
            }

        }



    }
}